package com.hm.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hm.cms.auto.su.BaseDomain;
import com.hm.cms.domain.QTermTaxonomyDomain;
import com.hm.cms.domain.TermTaxonomyDomain;
import com.hm.cms.repository.TermTaxonomyRepository;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.su.BeanUtil;
import com.hm.cms.vo.HcmsHomePageVo.WebPageNavVo;
import com.hm.cms.vo.HcmsTermTaxonomyVo;
import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;

/**
 * @author shishun.wang
 * @date 下午2:33:48 2017年10月13日
 * @version 1.0
 * @describe 标签
 */
@Service
public class TermTaxonomyServiceImpl implements TermTaxonomyService {

	@Autowired
	private TermTaxonomyRepository termTaxonomyRepository;

	@Override
	public String addTermTaxonomy(HcmsTermTaxonomyVo taxonomyVo, String operationUserId) {
		if (CommonUtil.isAnyEmpty(taxonomyVo.getTermTayCode(), taxonomyVo.getTermTayName(), taxonomyVo.getTermTayOrder())) {
			throw ServiceException.warning(ErrorCode.REQUIRED_PARAMETERS_MISSING);
		}

		QTermTaxonomyDomain qTermTaxonomyDomain = QTermTaxonomyDomain.termTaxonomyDomain;
		if (termTaxonomyRepository.exists(qTermTaxonomyDomain.termTayCode.eq(taxonomyVo.getTermTayCode())
				.or(qTermTaxonomyDomain.termTayName.eq(taxonomyVo.getTermTayName())))) {
			throw ServiceException.warning(ErrorCode.DATA_EXISTING);
		}

		TermTaxonomyDomain domain = new TermTaxonomyDomain();
		{
			BeanUtil.copyProperties(taxonomyVo, domain);
			BaseDomain.initBaseInfo(domain);
			domain.setCreateUser(operationUserId);
		}
		return termTaxonomyRepository.save(domain).getId();
	}

	@Override
	public HcmsTermTaxonomyVo getTermTaxonomysByCode(String termTayCode) {
		if (CommonUtil.isEmpty(termTayCode)) {
			throw ServiceException.warning(ErrorCode.PARAMETERS_MISSING);
		}
		TermTaxonomyDomain taxonomyDomain = termTaxonomyRepository
				.findOne(QTermTaxonomyDomain.termTaxonomyDomain.termTayCode.eq(termTayCode));
		if (CommonUtil.isEmpty(taxonomyDomain)) {
			return null;
		}
		HcmsTermTaxonomyVo vo = new HcmsTermTaxonomyVo();
		BeanUtil.copyProperties(taxonomyDomain, vo);
		return vo;
	}

	@Override
	public HcmsTermTaxonomyVo getTermTaxonomysById(String termTayId) {
		if (CommonUtil.isEmpty(termTayId)) {
			throw ServiceException.warning(ErrorCode.PARAMETERS_MISSING);
		}
		TermTaxonomyDomain taxonomyDomain = termTaxonomyRepository.findOne(termTayId);
		if (CommonUtil.isEmpty(taxonomyDomain)) {
			return null;
		}
		HcmsTermTaxonomyVo vo = new HcmsTermTaxonomyVo();
		BeanUtil.copyProperties(taxonomyDomain, vo);
		return vo;
	}

	@Override
	public List<HcmsTermTaxonomyVo> loadHcmsTermTaxonomys() {
		List<HcmsTermTaxonomyVo> vos = new ArrayList<HcmsTermTaxonomyVo>();

		QTermTaxonomyDomain qTermTaxonomyDomain = QTermTaxonomyDomain.termTaxonomyDomain;
		termTaxonomyRepository
				.findAll(qTermTaxonomyDomain.status.eq(DataStatusDef.ENABLE), qTermTaxonomyDomain.termTayOrder.desc())
				.forEach(termTaxonomys -> {
					HcmsTermTaxonomyVo vo = new HcmsTermTaxonomyVo();
					BeanUtil.copyProperties(termTaxonomys, vo);
					vos.add(vo);
				});
		return vos;
	}

	@Override
	public List<WebPageNavVo> loadSysNavs() {
		QTermTaxonomyDomain qTermTaxonomyDomain = QTermTaxonomyDomain.termTaxonomyDomain;
		List<WebPageNavVo> vos = new ArrayList<WebPageNavVo>();
		termTaxonomyRepository
				.findAll(qTermTaxonomyDomain.status.eq(DataStatusDef.ENABLE).and(qTermTaxonomyDomain.pageNav.eq(true)),
						qTermTaxonomyDomain.termTayOrder.asc())
				.forEach(termTaxonomys -> {
					if (!termTaxonomys.getPageNav()) {
						return;
					}
					WebPageNavVo vo = new WebPageNavVo();
					vo.setId(termTaxonomys.getId());
					vo.setName(termTaxonomys.getTermTayName());
					vo.setTermTayCode(termTaxonomys.getTermTayCode());
					//vo.setUri("o_term_taxonomy/tags/" + termTaxonomys.getTermTayCode());
					vo.setUri(termTaxonomys.getNavUri());

					vos.add(vo);
				});
		return vos;
	}

	@Override
	public List<WebPageNavVo> loadSysTermTaxos() {
		QTermTaxonomyDomain qTermTaxonomyDomain = QTermTaxonomyDomain.termTaxonomyDomain;
		List<WebPageNavVo> vos = new ArrayList<WebPageNavVo>();
		termTaxonomyRepository
				.findAll(qTermTaxonomyDomain.status.eq(DataStatusDef.ENABLE).and(qTermTaxonomyDomain.showHome.eq(true)))
				.forEach(termTaxonomys -> {
					WebPageNavVo vo = new WebPageNavVo();
					vo.setId(termTaxonomys.getId());
					vo.setName(termTaxonomys.getTermTayName());
					vo.setTermTayCode(termTaxonomys.getTermTayCode());
//					vo.setUri("o_term_taxonomy/tags/" + termTaxonomys.getTermTayCode());
					vo.setUri(termTaxonomys.getNavUri());

					vos.add(vo);
				});
		return vos;
	}

}
